<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>消息</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../css/main.css" media="all">
    <link rel="stylesheet" href="../../css/enterpriseChatPage.css" media="all">
    <link rel="stylesheet" href="../../css/ok/oksub.css">
    <link rel="stylesheet" href="../../css/enterpriseChat.css" media="all">
    <script type="text/javascript" src="../../libs/loading/okLoading.js"></script>
</head>


<body>
<!--主体内容-->
<div class="wai">
    <div style="width: 750px;margin: 0 auto" id="chatmsg">

    </div>
    <div class="nei">
        <textarea name="replmsg" placeholder="请输入消息" class="layui-textarea" maxlength="200" aria-placeholder="最多输入200字"
                  style="resize: none;font-size: 18px;height: 150px"></textarea>
        <button class="layui-btn" style="margin-top: 20px ;float: right;border-radius: 10px" id="sendmsg">发送</button>
    </div>
    <div style="height: 130px"></div>
</div>
</body>

<script src="../../js/model.js"></script>
<script src="../../libs/layui/layui.js"></script>
<script type="text/javascript">
    layui.use(['jquery', 'okLayer'], function () {
        let $ = layui.$;
        let okLayer = layui.okLayer;
        // 获取contentText
        const ctx = function () {
            let pathName = document.location.pathname;
            let index = pathName.substr(1).indexOf("/");
            let result = pathName.substr(0, index + 1);
            return result;
        }();
        // 获取enterprise
        var enterprise = JSON.parse(window.sessionStorage.getItem("enterprise"));
        var url = ctx + "/pages/enterprise/enterpriseLogin.html";
        if (enterprise == null) {
            okLayer.yellowSighMsg("登陆时间超时，请重新登录！", function () {
                window.sessionStorage.clear();
                window.open(url, "_top", "", "true");
            });
        }

        //获取参数列表
        let param = GetRequest();
        //绑定点击事件
        $("#sendmsg").click(function () {
            let msg = $("[name='replmsg']").val();
            var reply = new Reply(null, msg, enterprise.enterpriseid, null, new Record(param.recordid))
            $.ajax({
                url: ctx + '/ygssecs/addleaveAMessage',
                type: 'post',
                data: {replyjson: JSON.stringify(reply)},
                dataType: 'json',
                beforeSend: function () {
                    layer.load();
                }
            }).done(function (res) {
                layer.closeAll();
                if (res.code == 200) {
                    okLayer.greenTickMsg("回复成功", function () {
                        location.reload();
                    })
                } else if (res.code == 201) {
                    okLayer.yellowSighMsg(res.msg, function () {
                        location.reload();
                    })
                } else {
                    okLayer.redCrossMsg("哎呀，程序出错了，请刷新后重试！！", function () {
                        location.reload();
                    })
                }
            }).fail(function (e) {
                console.log(e)
            })
        })
        loadMessage();
        var imgurl;
        var studentname;

        //加载留言
        function loadMessage() {
            $.ajax({
                url: ctx + '/ygssecs/findRecordsById',
                type: 'get',
                data: {recordid: param.recordid},
                dataType: 'json',
            }).done(function (res) {
                okLoading.close()
                var leftStr = "";
                if (res.code == 200) {
                    var resdata = res.data;
                    imgurl = resdata.student.photo;
                    studentname = resdata.student.realname;
                    leftStr += "<div class='chat-sender'>";
                    leftStr += "  <div><img src='" + resdata.student.photo + "'/><\/div>";
                    leftStr += "  <div>" + resdata.student.realname + "<\/div>";
                    leftStr += "  <div>";
                    leftStr += "    <div class='chat-left_triangle'><\/div>\n";
                    leftStr += "    <span>" + resdata.selfrecommend + "<\/span>";
                    leftStr += "  <\/div>";
                    leftStr += "<\/div>";
                    $("#chatmsg").append(leftStr);
                    loadMessageList();
                }
            }).fail(function (e) {
                console.log(e)
            })
        }

        //加载留言列表
        function loadMessageList() {
            $.ajax({
                url: ctx + '/ygssecs/findReplysbyrecordid',
                type: 'get',
                data: {recordid: param.recordid},
                dataType: 'json'
            }).done(function (res) {
                if (res.code == 200) {
                    var replydata = res.data;
                    var resstr = "";
                    for (const reply of replydata) {
                        if (reply.respondentid == param.stuid) {
                            var leftStr = "";
                            leftStr += "<div class='chat-sender'>";
                            leftStr += "  <div><img src='" + imgurl + "'/><\/div>";
                            leftStr += "  <div>" + studentname + "<\/div>";
                            leftStr += "  <div>";
                            leftStr += "    <div class='chat-left_triangle'><\/div>\n";
                            leftStr += "    <span>" + reply.replymsg + "<\/span>";
                            leftStr += "  <\/div>";
                            leftStr += "<\/div>";
                            resstr += leftStr;
                        } else {
                            var rightStr = "";
                            rightStr += "<div class='chat-receiver'>";
                            rightStr += "  <div><img src='" + enterprise.logoimg + "' /><\/div>";
                            rightStr += "  <div>" + enterprise.enterprisename + "<\/div>";
                            rightStr += "  <div>";
                            rightStr += "    <div class='chat-right_triangle'><\/div>";
                            rightStr += "    <span>";
                            rightStr += reply.replymsg;
                            rightStr += "<\/span>";
                            rightStr += "  <\/div>";
                            rightStr += "<\/div>";
                            resstr += rightStr;
                        }
                    }
                    $("#chatmsg").append(resstr);
                }
            }).fail(function (e) {
                console.log(e)
            })
        }


        $(function () {
            var thisheight = $("#iframeblogT").contents().find("body").height() + 50;
            $("#iframeblogT").height(thisheight);
        })

        //获取参数列表
        function GetRequest() {
            var url = decodeURI(location.search); //?id="123456"&Name="bicycle";
            var object = {};
            if (url.indexOf("?") != -1)//url中存在问号，也就说有参数。
            {
                var str = url.substr(1);  //得到?后面的字符串
                var strs = str.split("&");  //将得到的参数分隔成数组[id="123456",Name="bicycle"];
                for (var i = 0; i < strs.length; i++) {
                    object[strs[i].split("=")[0]] = strs[i].split("=")[1];
                }
            }
            return object;
        }
    });
</script>
</html>